<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*,core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    table.fileAttachList { width: auto; margin-bottom: 0;}
    table.fileAttachList tr:first-child { background-color: #eee; }
    table.fileAttachList td:nth-child(1) { min-width: 400px; }
    table.fileAttachList td:nth-child(2) { text-align: right; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int articleId = param.getInt("aid", 0);

    Article article = ArticleDAO.selectById(articleId);
    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canReadArticle(article))
        new UnauthorizedAccessException("게시글 보기 권한 없음");

    ArrayList<File> list = FileDAO.selectByArticleId(articleId);
%>
<script>
    $(function() {
        if (window.parent) {
            var h = $("html").height();
            $('iframe[src*=file]', window.parent.document).css("height", h);
        }
    });
</script>
<body>

<% if (list.size() > 0) { %>
    <table class="table table-bordered table-condensed fileAttachList">
            <tr><td>첨부파일</td><td>파일크기</td><td></td></tr>
        <% for (File file : list) { %>
            <% String url = FileDownloadServlet.makeUrl(request, boardId, articleId, file.getId()); %>
            <tr>
                <td><%= file.getFileName() %></td>
                <td>
                    <% if (file.getFileSize() >= 10 * 1024 * 1024) { %>
                        <%= String.format("%d mb", file.getFileSize() / 1024 / 1024) %>
                    <% } else if (file.getFileSize() >= 10 * 1024) { %>
                        <%= String.format("%d kb", file.getFileSize() / 1024) %>
                    <% } else { %>
                        <%= file.getFileSize() %> bytes
                    <% } %>
                </td>
                <td>
                    <a class="btn btn-mini" href="<%=url%>">다운로드</a>
                </td>
            </tr>
        <% } %>
    </table>
<% } %>

</body>
</html>